package com.rykj.qxj.rest;

import com.github.pagehelper.PageInfo;
import com.rykj.qxj.server.model.entity.ApiManage;
import com.rykj.qxj.server.services.IApiManageService;
import com.rykj.qxj.server.services.IApplyManageService;
import com.rykj.qxj.server.util.date.DateUtils;
import com.rykj.qxj.server.util.object.StringUtils;
import com.rykj.qxj.util.Result;
import com.techindata.cacheredis.CacheZSet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Log4j2
@RestController
@RequestMapping("/v1/appApiVisit")
@Api(tags="应用接口调用次数")
public class AppApiVistController {

    @Autowired
    private IApplyManageService applyManageService;
    @Autowired
    private IApiManageService apiManageService;
    private static final String PER_API_VISITCOUNT_TODAY= "perApiVisitCount_today";
    @GetMapping("/getAppApiVisitCount")
    @ApiOperation(value="获取应用接口调用次数",notes ="获取应用接口调用次数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyId", value = "应用Id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "apiId", value = "接口id,多个逗号隔开",required = true,paramType = "query"),
            @ApiImplicitParam(name = "dataType", value = "查询时间类型:today,week,month,year",required = true,paramType = "query")

    })
    public Result getAppApiVisitCount(@RequestParam(value = "applyId") String applyId,
                                      @RequestParam(value = "apiId") String apiId,
                                      @RequestParam(value = "dataType") String dataType){
        try{
            String[] apiIds = null;
            if(StringUtils.isNotBlank(apiId)){
                apiIds = apiId.split(",");
            }
            List<Map<String,Object>> retlist = new ArrayList<>();
            if(dataType.equals("today")){
                List<ApiManage> apis = apiManageService.getApiInfoByIds(apiIds);
                for(ApiManage api : apis){
                    Map<String,Object> map = new HashMap<>();
                    map.put("apiName",api.getName());
                    String member = api.getUrl().replaceAll("/",":").replaceFirst(":","");
                    Long val = 0L;
                    String key = PER_API_VISITCOUNT_TODAY +  ":" + DateUtils.getDate(new Date(),"yyyy-MM-dd");
                    if (CacheZSet.zscore(key, member) != -1) {
                        val = new Double(CacheZSet.zscore(key, member)).longValue();
                    }
                    map.put("visitCount",val);
                    retlist.add(map);
                }

            }else{
                retlist= applyManageService.getAppApiVisitCount(applyId,apiIds,dataType);
            }

            return new Result(retlist);
        }catch (Exception e){
            log.error("获取应用接口调用次数失败", e);
            return new Result(false, "获取应用接口调用次数失败");
        }
    }

    @GetMapping("/findAppApiVisit")
    @ApiOperation(value="应用基本信息查询",notes ="应用基本信息查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "applyName", value = "应用名称",required = false,paramType = "query")
    })
    public Result findAppApiVisit(  @RequestParam(value = "pageNum") Integer pageNum,
                                    @RequestParam(value = "pageSize") Integer pageSize,
                                    @RequestParam(value = "applyName",required=false) String applyName){
        try{
            PageInfo pageInfo =  applyManageService.getApplyInfo(pageNum,pageSize,applyName);
            return new Result(pageInfo);
        }catch (Exception e){
            log.error("应用基本信息查询失败", e);
            return new Result(false, "应用基本信息查询失败");
        }
    }
}
